package com.kitty.game.fight.ai;

import com.kitty.game.config.Skill;
import com.kitty.common.utils.SpringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class NormalSkillChooser implements SkillChooser {

    private List<Integer> normalSkills = new ArrayList<>();

    private List<Integer> helpSkills = new ArrayList<>();

    private List<Integer> orderedSkills = new ArrayList<>();

    private Set<Integer> selectedSkills = new HashSet<>();

    public void init(List<Integer> allSkillIds) {
        for (Integer skillId : allSkillIds) {
            Skill skill = SpringUtils.getSkillService().getSkillInfo(skillId);
            if (skill == null) {
                continue;
            }
            if (skill.getSkillType() == 146) {//攻击
                normalSkills.add(skillId);
            } else {
                helpSkills.add(skillId);
            }
        }
    }

    private void reset() {
        orderedSkills.clear();

        Collections.shuffle(helpSkills);
        // 每次轮训只有一个是辅助或者障碍技能，其他均为攻击技能
        orderedSkills.addAll(normalSkills);
        if (helpSkills.size() > 0) {
            orderedSkills.add(helpSkills.get(0));
        }
        Collections.shuffle(orderedSkills);

        selectedSkills.clear();
    }

    @Override
    public int nextSkill(int round) {
        if (selectedSkills.size() < orderedSkills.size()) {
            int next = orderedSkills.get(selectedSkills.size());
            selectedSkills.add(next);
            return next;
        } else {
            reset();
            if (orderedSkills.size() <= 0) {
                return 0;
            }
            return nextSkill(round);
        }
    }

}

